package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.service.PrintLogDao;
import com.itheima.service.StudentService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService service;
    @Autowired
    private PrintLogDao printLogDao;

    @RequestMapping("/findByPage")
    public @ResponseBody PageInfo  findByPage(String currentPage, String pageSize) throws Exception {
        PageInfo page = service.findByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
         return page;
    }



    @RequestMapping("/addStu")
    public @ResponseBody Student addStu(HttpServletRequest request) throws Exception {
        Map<String, String[]> map = request.getParameterMap();
        Student student = new Student();
        dateConvert();
        BeanUtils.populate(student,map);
        service.insert(student);
        return student;
    }


    @RequestMapping("/editStu")
    public @ResponseBody Student update(HttpServletRequest request) throws Exception {
        ServletInputStream is = request.getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        dateConvert();
        Student stu = mapper.readValue(is, Student.class);
        System.out.println(stu);
        service.update(stu);
        return stu;
    }

    @RequestMapping("/deleteStu")
    @ResponseBody
    public void delete(String number){
        service.delete(number);
    }


    @RequestMapping("/getReport")
    @ResponseBody
    public void getReport(HttpServletResponse response,HttpServletRequest request)throws Exception{
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("日志.xlsx".getBytes(),"iso8859-1");
        response.addHeader("Content-Disposition","attachment;fileName="+fileName);
        ByteArrayOutputStream report =printLogDao.getReport();
        ServletOutputStream os = response.getOutputStream();
        report.writeTo(os);
        os.flush();
        report.close();

    }

    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            // 将参数value转换成Date对象并返回
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }
}
